package org.kabeja.dxf.entity;

import org.kabeja.dxf.DXFConstants;
import org.kabeja.dxf.helpers.Point;
import org.kabeja.math.MathUtils;
import org.kabeja.math.ParametricPlane;

/**
 * @author shutao
 * @since 2022/4/24 17:19
 */
public class DXFArc extends DXFEntity {
    private Point center = new Point();
    private double radius;
    private double startAngle;
    private double endAngle;
    private boolean counterclockwise = false;

    public DXFArc() {
        this.setLineType(this.getType());
    }

    public double getEndAngle() {
        return this.endAngle;
    }

    public void setEndAngle(double var1) {
        this.endAngle = var1;
    }

    public double getRadius() {
        return this.radius;
    }

    public void setRadius(double var1) {
        this.radius = var1;
    }

    public double getStartAngle() {
        return this.startAngle;
    }

    public void setStartAngle(double var1) {
        this.startAngle = var1;
    }

    @Override
    public Bounds getBounds() {
        Bounds var1 = new Bounds();
        Point var2 = this.getStartPoint();
        Point var3 = this.getEndPoint();
        var1.addToBounds(var2);
        var1.addToBounds(var3);
        ParametricPlane var4 = new ParametricPlane(this.getExtrusion());
        Point var5 = var4.getPoint(this.center.getX(), this.center.getY());
        int var6 = MathUtils.getQuadrant(var2, var5);
        int var7 = MathUtils.getQuadrant(var3, var5);
        if (var7 < var6) {
            var7 += 4;
        }

        for(; var7 > var6; ++var6) {
            switch(var6) {
                case 0:
                    var1.addToBounds(var5.getX(), var5.getY() + this.radius, var5.getZ());
                    break;
                case 1:
                    var1.addToBounds(var5.getX() - this.radius, var5.getY(), var5.getZ());
                    break;
                case 2:
                    var1.addToBounds(var5.getX(), var5.getY() - this.radius, var5.getZ());
                    break;
                case 3:
                    var1.addToBounds(var5.getX() + this.radius, var5.getY(), var5.getZ());
                    var7 -= 4;
                    var6 -= 4;
                    break;
                default:
                    break;
            }
        }

        return var1;
    }

    public void setCenterPoint(Point var1) {
        this.center = var1;
    }

    public Point getCenterPoint() {
        return this.center;
    }

    public Point getStartPoint() {
        double var1 = this.startAngle;
        return this.getPointAt(var1);
    }

    public Point getEndPoint() {
        double var1 = this.endAngle;
        return this.getPointAt(var1);
    }

    public Point getPointAt(double var1) {
        double var3 = this.radius * Math.cos(Math.toRadians(var1));
        double var5 = this.radius * Math.sin(Math.toRadians(var1));
        ParametricPlane var7 = new ParametricPlane(this.getExtrusion());
        return var7.getPoint(var3 + this.center.getX(), var5 + this.center.getY());
    }

    @Override
    public String getType() {
        return DXFConstants.ENTITY_TYPE_ARC;
    }

    @Override
    public double getLength() {
        double var1 = this.getTotalAngle();
        return var1 * 3.141592653589793D * this.radius / 180.0D;
    }

    public double getTotalAngle() {
        return this.endAngle < this.startAngle ? 360.0D + this.endAngle - this.startAngle : Math.abs(this.endAngle - this.startAngle);
    }

    public double getChordLength() {
        return 2.0D * this.radius * Math.sin(Math.toRadians(this.getTotalAngle() / 2.0D));
    }

    public boolean isCounterClockwise() {
        return this.counterclockwise;
    }

    public void setCounterClockwise(boolean var1) {
        this.counterclockwise = var1;
    }
}
